#!/usr/bin/env python
# encoding: utf-8
from feature_set.loan.un.loan_un_base_v3_2.loan_un_installment_features_v3_2 import *


class LoanUnPayoffv3_2(object):

    @staticmethod
    def get_features(rst_data):
        current_info = rst_data['current_info']
        current_apply_time = current_info['current_apply_time']
        current_acq_channel = current_info['current_acq_channel']
        current_loan_amount = current_info['current_loan_amount']
        current_product_code = current_info['current_product_code']
        register_time = current_info['register_time']
        user_history_order = rst_data['user_history_order']
        user_history_installment = rst_data['user_history_installment']

        user_history_installment = user_history_installment[user_history_installment.bill_category.str.contains('payoff')]  # 筛选符合还清条件的数据

        all_bill_features = {}
        # 获取还清订单特征
        for feature_type in ['all', 'self_app', 'other_app', 'self_product', 'other_product']:
            first_payoff_features = get_first_payoff_features(user_history_installment,
                                                              user_history_order,
                                                              payoff_type='payoff',
                                                              current_apply_time=current_apply_time,
                                                              current_loan_amount=current_loan_amount,
                                                              register_time=register_time,
                                                              feature_type=feature_type)
            all_bill_features.update(first_payoff_features)

            last_payoff_features = get_last_payoff_features(user_history_installment,
                                                            user_history_order,
                                                            payoff_type='payoff',
                                                            current_apply_time=current_apply_time,
                                                            current_loan_amount=current_loan_amount,
                                                            register_time=register_time,
                                                            feature_type=feature_type)
            all_bill_features.update(last_payoff_features)

            day_payoff_features = get_day_payoff_features(user_history_installment,
                                                          payoff_type='payoff',
                                                          day_list=[1, 3, 7, 15, 30, 60, 90, 180, 'all'],
                                                          records_list=[1, 3, 7, 15, 30],
                                                          current_apply_time=current_apply_time,
                                                          feature_type=feature_type)
            all_bill_features.update(day_payoff_features)

            app_prod_payoff_features = get_app_prod_payoff_features(user_history_installment,
                                                                    payoff_type='payoff',
                                                                    day_list=[1, 3, 7, 15, 30, 60, 90, 180, 'all'],
                                                                    records_list=[1, 3, 7, 15, 30],
                                                                    current_apply_time=current_apply_time,
                                                                    current_acq_channel=current_acq_channel,
                                                                    current_product_code=current_product_code,
                                                                    feature_type=feature_type)
            all_bill_features.update(app_prod_payoff_features)

            agg_day_features = get_agg_day_features(user_history_installment,
                                                    user_history_order,
                                                    payoff_type='payoff',
                                                    day_list=[1, 3, 7, 15, 30, 60, 90, 180, 'all'],
                                                    current_apply_time=current_apply_time,
                                                    feature_type=feature_type)
            all_bill_features.update(agg_day_features)

            continue_acc_features = get_continue_acc_feature(user_history_installment,
                                                             payoff_type='payoff',
                                                             feature_type=feature_type)
            all_bill_features.update(continue_acc_features)

        for key, value in all_bill_features.items():
            all_bill_features[key] = float(value) if not np.isnan(value) else -999.0

        return all_bill_features
